<?php
/**
 *  
 *
  */
namespace Org\Util;
class Rgxklsf {

    
     public function gxklsf_balance() {
		 $day=date("Y-m-d");
		 $day_time=strtotime($day);
		 $insert_data=array();
		 $lm=array();
		// foreach($open_time as $k=>$v){
// 				  $kj_time[$k]=strtotime($day.' '.$v['kj_time']);
				  $n_time=time(); 
				 // if($n_time>$kj_time[$k]&&$n_time<$kj_time[$k]+60*3){  //根据后台开始将时间之后3分钟内不停的获取开奖数据
				    $condition['c_id']=10;	
					//$condition['add_time'] = array(array('gt',$day_time)) ; 
 					$max_c_no=M('lottery_results')->where($condition)->max('c_no'); //获取当天最大期数
					 
					$url='http://a.apiplus.net/newly.do?token=5ee5aca63461f954&code=gxklsf&rows=1&format=json'; //接口地址
					
					$new_result=get_http($url);
					$result=json_decode($new_result,true);
					 
				
					if($max_c_no!=$result['data'][0]['expect']){
				 	// print_r($result['data'][0]['expect']);exit;
					S('gxklsf_'.$result['data'][0]['expect'],$result['data'][0]['opencode']);  //存入缓存  方便计算两面长龙算法
					$insert_data['c_no']=$result['data'][0]['expect'];
					$insert_data['c_id']=10;
					$insert_data['c_name']='广西快乐十分';
					$insert_data['add_time']=$result['data'][0]['opentimestamp'];
					$insert_data['lottery_result']=$result['data'][0]['opencode'];
					$insert_data['status']=0;
					$num= explode(",",$result['data'][0]['opencode']);
					  	foreach($num as $k => $v){
						$num[$k]=preg_replace('/^0+/','',$v);   //去除左边的0
						}
					$sum=array_sum($num);
					$lm[0]=$sum;
					$lm[1]=$this->zh_ds($sum);
		            $lm[2]=$this->zh_dx($sum);
		           // $lm[3]=$this->zh_w_dx($sum);	 
		            $lm[3]=$this->lht($num);	 
				    $n_lm=implode(",",$lm);
					$insert_data['lm']=$n_lm;
					if($max_c_no+1==$result['data'][0]['expect']){  //计算两面长龙结果
					  
					  $bdx=$this->bdx($num); //1-5大小
					  $bds=$this->bds($num); //1-5单双 
					  $bdx_w=$this->bdx_w($num); //1-5合数大小 
					  $bds_h=$this->bds_h($num); //1-5尾单双 
					  $color=$this->color($num); //1-5波色 
					  $fulsx=$this->fulsx($num); //1-5福禄寿喜
					 
 					   $flag=1;
 					 
					     
					    for($i=1;$i<=5;$i++){
						  $strds='flag_bds'.$i;
					      $$strds=0;
						  $strds='flag_bds_h'.$i;
					      $$strds=0;
						   
						  $strdx='flag_bdx'.$i;
					      $$strdx=0;
						  
						  $strdx='flag_bdx_w'.$i;
					      $$strdx=0;
						  
						  $strdx='flag_bdx_w'.$i;
					      $$strdx=0;
						  
						  $str_lh='flag_lh'.$i;
					      $$str_lh=0;
					   }
					   $k=1;
					   do{
						   
						   $old_lottery=$result['data'][0]['expect']-$k;
						   //S('bjpk10_'.$old_lottery,NULL);
						   $cqnc_result=S('gxklsf_'.$old_lottery);
			 
							if(empty($cqnc_result))
							{
							$condition1['c_id']=10;
							$condition1['c_no']=$old_lottery;
							$cqnc_result=M('lottery_results')->where($condition1)->getField('lottery_result');
							if($cqnc_result1){
							S('gxklsf_'.$old_lottery,$cqnc_result);
							}else{
								$flag=0;
								}
							}	
						  
 						   $num1[$k-1]= explode(",",$cqnc_result);
						  $sum1[$k-1]=array_sum($num1[$k-1]);
						 $bds1=$this->bds($num1[$k-1]);
						 for($i=1;$i<=5;$i++){
						  $str='flag_bds'.$i;
						  $str1='b_ds'.$i;	
						  switch ($i)
							{
							 case 1:$gread='第一球';
							 break;
							 case 2:$gread='第二球';
							 break;
							 case 3:$gread='第三球';
							 break;
							 case 4:$gread='第四球';
							 break;
							 case 5:$gread='第五球';
							 break;
							
						
							  default:
 									break;
								
							  }
						   if($$str==0){
							   $ab=$i-1;
							  if($bds1[$ab]==$bds[$ab]){
								 
							   $b=$k+1;
								 if($bds1[$ab]=='双'){
								 $$str1='DS'.$i.'=S,'.$b.','.$gread.'-双';
								 }else{
								 $$str1='DS'.$i.'=D,'.$b.','.$gread.'-单';
									 }
							   }else{
							   $$str=1;	 
								   }
						   }
						  }
						   
						  $bdx1=$this->bdx($num1[$k-1]);
						
						 for($i=1;$i<=5;$i++){
						  $str2='flag_bdx'.$i;
						  $str3='b_dx'.$i;	
						  switch ($i)
							{
							  case 1:$gread='第一球';
							 break;
							 case 2:$gread='第二球';
							 break;
							 case 3:$gread='第三球';
							 break;
							 case 4:$gread='第四球';
							 break;
							 case 5:$gread='第五球';
							 break;
							
							
							  default:
 									break;
								
							  }
						   if($$str2==0){
							   $ab=$i-1;
							  if($bdx1[$ab]==$bdx[$ab]){
								 
							   $b=$k+1;
								 if($bdx1[$ab]=='小'){
								 $$str3='DX'.$i.'=X,'.$b.','.$gread.'-小';
								 }else{
								 $$str3='DX'.$i.'=D,'.$b.','.$gread.'-大';
									 }
							   }else{
							   $$str2=1;	 
								   }
						   }
						  } 
						 
						   //尾大小
						    $bdx1_w=$this->bdx_w($num1[$k-1]);
					
						 for($i=1;$i<=5;$i++){
						  $str4='flag_bdx_w'.$i;
						  $str5='b_dx_w'.$i;	
						  switch ($i)
							{
							  case 1:$gread='第一球';
							 break;
							 case 2:$gread='第二球';
							 break;
							 case 3:$gread='第三球';
							 break;
							 case 4:$gread='第四球';
							 break;
							 case 5:$gread='第五球';
							 break;
							
							
							  default:
 									break;
								
							  }
						   if($$str4==0){
							   $ab=$i-1;
							  if($bdx1_w[$ab]==$bdx_w[$ab]){
								 
							   $b=$k+1;
								 if($bdx1_w[$ab]=='尾小'){
								 $$str5='WDX'.$i.'=X,'.$b.','.$gread.'-尾小';
								 }else{
								 $$str5='WDX'.$i.'=D,'.$b.','.$gread.'-尾大';
									 }
							   }else{
							   $$str4=1;	 
								   }
						   }
						  } 
						
						  //合数
						    $bds1_h=$this->bds_h($num1[$k-1]);
						
						 for($i=1;$i<=5;$i++){
						  $str6='flag_bds_h'.$i;
						  $str7='b_ds_h'.$i;	
						  switch ($i)
							{
							  case 1:$gread='第一球';
							 break;
							 case 2:$gread='第二球';
							 break;
							 case 3:$gread='第三球';
							 break;
							 case 4:$gread='第四球';
							 break;
							 case 5:$gread='第五球';
							 break;
							
							
							  default:
 									break;
								
							  }
						    if($$str6==0){
							   $ab=$i-1;
							  if($bds1_h[$ab]==$bds_h[$ab]){
								 
							   $b=$k+1;
								 if($bds_h[$ab]=='合双'){
								 $$str7='HDS'.$i.'=S,'.$b.','.$gread.'-合双';
								 }else{
								 $$str7='HDS'.$i.'=D,'.$b.','.$gread.'-合单';
									 }
							   }else{
							   $$str6=1;	 
								   }
						   }
						  } 
						   //波色
						    $color1=$this->color($num1[$k-1]);
						
						 for($i=1;$i<=5;$i++){
						  $str8='flag_color'.$i;
						  $str9='b_color'.$i;	
						  switch ($i)
							{
							  case 1:$gread='第一球';
							 break;
							 case 2:$gread='第二球';
							 break;
							 case 3:$gread='第三球';
							 break;
							 case 4:$gread='第四球';
							 break;
							 case 5:$gread='第五球';
							 break;
							
							
							  default:
 									break;
								
							  }
						    if($$str8==0){
							   $ab=$i-1;
							  if($color1[$ab]==$color[$ab]){
								 
							   $b=$k+1;
								 if($color1[$ab]=='红波'){
								 $$str9='SB'.$i.'=R,'.$b.','.$gread.'-红波';
								 }elseif($color1[$ab]=='蓝波'){
								 $$str9='SB'.$i.'=B,'.$b.','.$gread.'-蓝波';
									 }else{
										 $$str9='SB'.$i.'=G,'.$b.','.$gread.'-绿波'; 
									 }
							   }else{
							   $$str8=1;	 
								   }
						   }
						  }
						  
						     //福禄寿喜
						    $fulsx1=$this->fulsx($num1[$k-1]);
						
						 for($i=1;$i<=5;$i++){
						  $str10='flag_flsx'.$i;
						  $str11='b_flsx'.$i;	
						  switch ($i)
							{
							  case 1:$gread='第一球';
							 break;
							 case 2:$gread='第二球';
							 break;
							 case 3:$gread='第三球';
							 break;
							 case 4:$gread='第四球';
							 break;
							 case 5:$gread='第五球';
							 break;
							
							
							  default:
 									break;
								
							  }
						    if($$str10==0){
							   $ab=$i-1;
							  if($fulsx1[$ab]==$fulsx[$ab]){
								 
							   $b=$k+1;
								 if($fulsx1[$ab]=='福'){
								 $$str11='YFLSX'.$i.'=F,'.$b.','.$gread.'-福';
								 }elseif($fulsx1[$ab]=='禄'){
								 $$str11='YFLSX'.$i.'=L,'.$b.','.$gread.'-禄';
									 }elseif($fulsx1[$ab]=='寿'){
								 $$str11='YFLSX'.$i.'=S,'.$b.','.$gread.'-寿';
									 }else{
										 $$str11='YFLSX'.$i.'=X,'.$b.','.$gread.'-喜'; 
									 }
							   }else{
							   $$str10=1;	 
								   }
						   }
						  }
						
						 
						    //计算总和单双两面长龙
						   $ds1=$this->zh_ds($sum1[$k-1]);
						   if($flag_zds==0){
							   
							  if($ds1==$this->zh_ds($sum)){
							   $b=$k+1;
							   //ZDS=D,2,总和单
								 if($ds1=='双'){
								 $zh_ds='ZDS=S,'.$b.',总和双';
								 }else{
								 $zh_ds='ZDS=D,'.$b.',总和单';
									 }
							   }else{
							   $flag_zds=1;	 
								   }
						   }
						    //计算总和大小两面长龙
						   $dx1=$this->zh_dx($sum1[$k-1]);
						   if($flag_zdx==0){
							   
							  if($dx1==$this->zh_dx($sum)){
							   $b=$k+1;
							   //ZDS=D,2,总和单
								 if($dx1=='小'){
								 $zh_dx='ZDX=X,'.$b.',总和小';
								 }else{
										 //$zh_dx='ZDX=H,'.$b.',总和和'; 
										  $zh_dx='ZDX=D,'.$b.',总和大';
									 }
							   }else{
							   $flag_zdx=1;	 
								   }
						   }
						    //计算总和尾大小两面长龙
						   $dx1_w=$this->zh_w_dx($sum1[$k-1]);
						   if($flag_zdx_w==0){
							   
							  if($dx1_w==$this->zh_w_dx($sum)){
							   $b=$k+1;
							  
								 if($dx1_w=='尾小'){
								 $zh_dx_w='ZWDX=X,'.$b.',总和尾数小';
								 }else{
								 $zh_dx_w='ZWDX=D,'.$b.',总和尾数大';
									 }
							   }else{
							   $flag_zdx_w=1;	 
								   }
						   }
						 
						 $k++;	 
					      $sss1='flag_bds1';
						  $sss2='flag_bds2';
						  $sss3='flag_bds3';
						  $sss4='flag_bds4';
						  $sss5='flag_bds5';
						 
						 
						  
						  $xxx1='flag_bdx1';
						  $xxx2='flag_bdx2';
						  $xxx3='flag_bdx3';
						  $xxx4='flag_bdx4';
						  $xxx5='flag_bdx5';
						 
						  
						  $sss1_h='flag_bds_h1';
						  $sss2_h='flag_bds_h2';
						  $sss3_h='flag_bds_h3';
						  $sss4_h='flag_bds_h4';
						  $sss5_h='flag_bds_h5';
						  
						  
						   $xxx1_w='flag_bdx_w1';
						  $xxx2_w='flag_bdx_w2';
						  $xxx3_w='flag_bdx_w3';
						  $xxx4_w='flag_bdx_w4';
						  $xxx5_w='flag_bdx_w5';
						  
						  
						   $color1_w='flag_color1';
						   $color2_w='flag_color2';
						   $color3_w='flag_color3';
						   $color4_w='flag_color4';
						   $color5_w='flag_color5';
						   
						   $flsx1_w='flag_flsx1';
						   $flsx2_w='flag_flsx2';
						   $flsx3_w='flag_flsx3';
						   $flsx4_w='flag_flsx4';
						   $flsx5_w='flag_flsx5';
					
						 
						  if($flag_zds==1&&$flag_zdx_w==1&&$flag_zdx==1&&$$xxx1==1&&$$xxx2==1&&$$xxx3==1&&$$xxx4==1&&$$xxx5==1&&$$sss1==1&&$$sss2==1&&$$sss3==1&&$$sss4==1&&$$sss5==1&&$$sss1_h==1&&$$sss2_h==1&&$$sss3_h==1&&$$sss4_h==1&&$$sss5_h==1&&$$xxx1_w==1&&$$xxx2_w==1&&$$xxx3_w==1&&$$xxx4_w==1&&$$xxx5_w==1&&$$flsx1_w&&$$flsx2_w&&$$flsx3_w&&$$flsx4_w&&$$flsx5_w&&$$color1_w&&$$color2_w&&$$color3_w&&$$color4_w&&$$color5_w||$k>10){
							// if($k<11){
							   $flag=0;
							   }
							 //
							 
						
							
						   }while($flag==1);
						
					
					   for($i=1;$i<=5;$i++){
		                 $strx='b_ds'.$i;
			             if($$strx){	 
					     $s[]=$$strx;	
					      }
						  
						  $strbdx='b_dx'.$i;
			              if($$strbdx){	 
					       $s[]=$$strbdx;	
					      }
						  //合数
						    $strbds_h='b_ds_h'.$i;
			              if($$strbds_h){	 
					       $s[]=$$strbds_h;	
					      }
						   //尾大小
						    $strbdx_w='b_dx_w'.$i;
			              if($$strbdx_w){	 
					       $s[]=$$strbdx_w;	
					      }
						  
						   //波色
						    $strbdx_c='b_flsx'.$i;
			              if($$strbdx_c){	 
					       $s[]=$$strbdx_c;	
					      }
						    //福禄寿喜
						    $strbdx_f='b_color'.$i;
			              if($$strbdx_f){	 
					       $s[]=$$strbdx_f;	
					      }
						  
					   }
					   
					
					   
						$s[]=$zh_ds;	
						    
					    $s[]=$zh_dx;
						  
					    $s[]=$zh_dx_w;
						
						
						//print_r($s);exit;
						 
					   $n_s=implode(";",$s);	
						
					  $insert_data['detail']=$n_s;	
						}
					
					$insert_result=M('lottery_results')->data($insert_data)->add();	
				
				 	if(!$insert_result){
						 
						 file_put_contents("test.txt", date("Y-m-d H:i:s") . "获取广西快乐十分结果存入失败！" . "\r\n<br>", FILE_APPEND);  	
							}else{
						$gxklsf_result = new \Org\Util\Jsgxklsf(); //广西快乐十分
        $gxklsf_result->gxklsf_balance();
					}
						
 								
					}
					 
		return; 		
     }
	
 public function zh_ds($sum){  //总和单双
      if($sum%2==0){
		$ds='双';
			}else{
		$ds='单';	 
		}
		return $ds;
 }
 public function zh_dx($sum){  //总和大小
      if($sum>=55){
		   $dx='大';	 
			 }else{
			 $dx='小';	 
				 }
		return $dx;
 }
  public function zh_w_dx($sum){  //总和尾大小
     $w=substr($sum,-1);

	 if($w>=5){
		   $dx='尾大';	 
			 }else{
			 $dx='尾小';	 
				 }
		return $dx;
 }
 
 public function lht($num){  //总和龙虎

      if($num[0]>$num[4]){
			$lht='龙';	 
			 }elseif($num[0]<$num[4]){
				$lht='虎';	  
				 }	else{
				$lht='和';	  	 
					 }	
		return $lht;
 }
 public function bdx($num){  //1-5球大小
	 $bdx=array();
	 foreach($num as $k=>$v){
		 if($v>=11){
		   $bdx[$k]='大';	 
			 }else{
			$bdx[$k]='小';	 
				 }
 		 }
	return $bdx;	 
  }
   public function bdx_w($num){  //1-5球尾大小
	 $wdx=array();
	 foreach($num as $k=>$v){
		  $w=substr($v,-1);

	 if($w>4){
		   $wdx[$k]='尾大';	 
			 }else{
			 $wdx[$k]='尾小';	 
				 }
		
 		 }
	return $wdx;	 
  }
 public function bds($num){  //1-5球单双
	 $bds=array();
	 foreach($num as $k=>$v){
		 if($v%2==0){
		   $bds[$k]='双';	 
			 }else{
			$bds[$k]='单';	 
				 }
 		 }
	return $bds;	 
  }	 
 public function bds_h($num){  //1-5球合数单双
	 $bds=array();
	 foreach($num as $k=>$v){
		 //个位
		 $g=$v%10;
		 $shi=$v/10%10;
		 $sum=$g+$shi;
		 if($sum%2==0){
		   $bds[$k]='合双';	 
			 }else{
			$bds[$k]='合单';	 
				 }
 		 }
	return $bds;	 
  }
 public function color($num){  //1-5球红波绿波蓝波
	 $bds=array();
	 $red=array(1,4,7,10,13,16,19);
	 $green=array(3,6,9,12,15,18,21);
	 $blue=array(2,5,8,11,14,17,20);
	 foreach($num as $k=>$v){
		if(in_array($v,$red)){
			$bds[$k]='红波';
		}elseif(in_array($v,$green)){
			$bds[$k]='绿波';
		}else{
			$bds[$k]='蓝波';
		}
 		 }
	return $bds;	 
  }
   public function fulsx($num){  //1-5球福禄寿喜
	 $bds=array();
	 
	 foreach($num as $k=>$v){
		if($v>=0&&$v<=5){
		$bds[$k]='福';	
		}elseif($v>=6&&$v<=10){
			$bds[$k]='禄';
		}elseif($v>=11&&$v<=15){
			$bds[$k]='寿';
		}elseif($v>=16&&$v<=20){
			$bds[$k]='喜';
		}else{
			$bds[$k]='和';
			}
 		 }
	return $bds;	 
  }  
	 
}